카메라 미리보기를 텍스처로 렌더링하는 방법 :: OPEN GL ES[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

OPEN GL ES
[1]
등록일:2018-06-24 15:56:00 (0%)
작성자:
제목:카메라 미리보기를 텍스처로 렌더링하는 방법
Android에서 비디오 앱을 개발하고 쉐이더를 통해 카메라에서 비디오를 렌더링하고 필터링하기 위해 OpenGL을 사용합니다. 내 셰이더 중 하나는 두 개의 텍스처를 사용합니다. 하나는 카메라의 라이브 프레임이고 다른 하나는 이전 렌더링 패스 결과입니다. 따라서 그림을 두 번 렌더링해야합니다. 미리보기를 위해 화면을 표시하고 다음 렌더 패스를 위해 텍스처를 분리해야합니다. 그것을 달성하기 위해 프레임 버퍼 객체를 사용합니다. 여기 내 코드가 있습니다 :

초기화 프레임 버퍼 :

  1. private void initPredrawBuffer() {
  2. GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
  3. prevBuffer = new int[1];
  4. GLES20.glGenFramebuffers(1, prevBuffer, 0);
  5. GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, prevBuffer[0]);
  6. prevTexture = new int[1];
  7. GLES20.glGenTextures(1, prevTexture, 0);
  8. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, prevTexture[0]);
  9. GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
  10. GLES20.GL_NEAREST);
  11. GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
  12. GLES20.GL_LINEAR);
  13. GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
  14. GLES20.GL_CLAMP_TO_EDGE);
  15. GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
  16. GLES20.GL_CLAMP_TO_EDGE);
  17. GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mFrameWidth, mFrameHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
  18. GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, prevTexture[0], 0);
  19. int[] depthRenderbuffers = new int[1];
  20. GLES20.glGenRenderbuffers(1, depthRenderbuffers, 0);
  21. GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, depthRenderbuffers[0]);
  22. GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, mFrameWidth, mFrameHeight);
  23. GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, depthRenderbuffers[0]);
  24. int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
  25. if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
  26. throw new RuntimeException("Incomplete buffer");
  27. }
  28. }

및 렌더링 :

  1. private void draw(boolean offScreen, float[] mvpMatrix, FloatBuffer vertexBuffer, int firstVertex,
  2. int vertexCount, int coordsPerVertex, int vertexStride,
  3. float[] texMatrix, FloatBuffer texBuffer, int textureId, int texStride) {
  4. GlUtil.checkGlError("draw start");
  5. if (offScreen)
  6. GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, prevBuffer[0]);
  7. else
  8. GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
  9. // Select the program.
  10. GLES20.glUseProgram(firstFrame && firstFrameProgram > 0 ? firstFrameProgram : mProgramHandle);
  11. GlUtil.checkGlError("glUseProgram");
  12. // Set the texture.
  13. GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  14. GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId);
  15. // Copy the model / view / projection matrix over.
  16. GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mvpMatrix, 0);
  17. GlUtil.checkGlError("glUniformMatrix4fv");
  18. // Copy the texture transformation matrix over.
  19. GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, texMatrix, 0);
  20. GlUtil.checkGlError("glUniformMatrix4fv");
  21. // Enable the "aPosition" vertex attribute.
  22. GLES20.glEnableVertexAttribArray(maPositionLoc);
  23. GlUtil.checkGlError("glEnableVertexAttribArray");
  24. // Connect vertexBuffer to "aPosition".
  25. GLES20.glVertexAttribPointer(maPositionLoc, coordsPerVertex,
  26. GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
  27. GlUtil.checkGlError("glVertexAttribPointer");
  28. // Enable the "aTextureCoord" vertex attribute.
  29. GLES20.glEnableVertexAttribArray(maTextureCoordLoc);
  30. GlUtil.checkGlError("glEnableVertexAttribArray");
  31. // Connect texBuffer to "aTextureCoord".
  32. GLES20.glVertexAttribPointer(maTextureCoordLoc, 2,
  33. GLES20.GL_FLOAT, false, texStride, texBuffer);
  34. GlUtil.checkGlError("glVertexAttribPointer");
  35. // Setting Light Trails filter params
  36. if (mProgramType == ProgramType.TEXTURE_LIGHT_TRAILS && !firstFrame) {
  37. GLES20.glUniform1i(muTextureNew, 0);
  38. GlUtil.checkGlError("glUniform1i muTextureNew");
  39. GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
  40. GlUtil.checkGlError("temp glActiveTexture");
  41. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, prevTexture[0]);
  42. GlUtil.checkGlError("temp glBindTexture");
  43. GLES20.glUniform1i(muTexturePrev, 1);
  44. GlUtil.checkGlError("glUniform1i muTexturePrev");
  45. GLES20.glUniform1f(muLightTrailsExposure, lightTrailsKernel[0]);
  46. GlUtil.checkGlError("glUniform1f muLightTrailsExposure");
  47. GLES20.glUniform1f(muLightTrailsEcho, lightTrailsKernel[1]);
  48. GlUtil.checkGlError("glUniform1f muLightTrailsEcho");
  49. GLES20.glUniform1f(muLightTrailsLimit, lightTrailsKernel[2]);
  50. GlUtil.checkGlError("glUniform1f muLightTrailsLimit");
  51. GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  52. GlUtil.checkGlError("temp glActiveTexture");
  53. }
  54. // Draw the rect.
  55. GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, firstVertex, vertexCount);
  56. GlUtil.checkGlError("glDrawArrays");
  57. GLES20.glFinish();
  58. }

문제는 프레임 버퍼에 렌더링이 작동하지 않고 glReadPixels 항상 빈 배열을 반환한다는 것입니다. 화면에 렌더링이 잘 작동합니다.

[본문링크] 카메라 미리보기를 텍스처로 렌더링하는 방법
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=34669
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.